﻿using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SXFramWork.Core
{
    public class MessageHub:Hub
    {
        private readonly ILoginInfo _loginInfo;
        private ICache _cache;
        public MessageHub(ILoginInfo loginInfo,ICache cache)
        {
            _cache = cache;
            _loginInfo = loginInfo;
        }
        public override async Task OnConnectedAsync()
        {
            //一个用户一个分组
            var uid = _cache.Get("uid");
            if (uid.IsNull())
                uid = _loginInfo.UserId;
            Console.WriteLine("当前用户id：" + uid + "||||connid:" + Context.ConnectionId);
            if (uid.NotNull())
            {
                await Groups.AddToGroupAsync(Context.ConnectionId, uid);
            }

        }
        /// <summary>
        /// 加入指定组
        /// </summary>
        /// <param name="groupName">组名</param>
        /// <returns></returns>
        public async Task AddToGroup(string groupName)
        {
            await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
        }

        public override async Task OnDisconnectedAsync(Exception exception)
        {
            await base.OnDisconnectedAsync(exception);
        }
    }
}
